<!-- src/components/Header.vue -->
<template>
  <header class="header">
    <div class="container">
      <div class="header-content">
        <div class="logo" @click="goHome">
          <h1>🍶 WineShop</h1>
        </div>

        <nav class="nav">
          <router-link v-if="authStore.userRole === 'admin'" to="/admin" class="nav-link" active-class="active">管理后台</router-link>
        </nav>

        <div class="header-actions" style="display: flex; align-items: center; gap: 10px">
          <!-- 搜索按钮图标 -->
          <el-tooltip content="搜索商品" placement="bottom">
            <el-button class="icon-btn square-icon" @click="goToSearch">
              <el-icon><Search /></el-icon>
            </el-button>
          </el-tooltip>

          <!-- 未登录时显示注册与登录 -->
          <el-tooltip content="注册" placement="bottom" v-if="!authStore.isAuthenticated">
            <router-link to="/register">
              <el-button class="icon-btn square-icon">
                <el-icon><EditPen /></el-icon>
              </el-button>
            </router-link>
          </el-tooltip>

          <el-tooltip content="登录" placement="bottom" v-if="!authStore.isAuthenticated">
            <router-link to="/login">
              <el-button class="icon-btn square-icon" circle>
                <el-icon><User /></el-icon>
              </el-button>
            </router-link>
          </el-tooltip>

          <!-- 已登录时显示退出 -->
          <el-tooltip content="退出登录" placement="bottom" v-if="authStore.isAuthenticated">
            <el-button class="icon-btn" circle type="danger" @click="logout">
              <el-icon><SwitchButton /></el-icon>
            </el-button>
          </el-tooltip>
        </div>
      </div>
    </div>
  </header>
</template>

<script setup>
import { Search, EditPen, User, SwitchButton } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/store/auth'

const router = useRouter()
const authStore = useAuthStore()

const goHome = () => {
  router.push('/')
}

const goToSearch = () => {
  router.push('/search')
}

const logout = () => {
  authStore.logout()
  router.push('/login')
}
</script>

<style scoped>
.header {
  background: linear-gradient(135deg, #8b4513 0%, #a0522d 100%);
  color: white;
  padding: 1rem 0;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 1rem;
}

/* 正方形图标按钮样式 */
.icon-btn.square-icon {
  background: linear-gradient(135deg, #8b4513 0%, #a0522d 100%);
  color: white;
  border: none;
  width: 48px;
  height: 48px;
  font-size: 22px;
  padding: 0;
  border-radius: 8px; /* 轻微圆角，也可以设为 0 */
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.3s, background 0.3s;
}

.icon-btn.square-icon:hover {
  transform: scale(1.1);
  background: linear-gradient(135deg, #a0522d 0%, #8b4513 100%);
}

.logo {
  cursor: pointer;
  transition: transform 0.3s;
}

.logo:hover {
  transform: scale(1.05);
}

.logo h1 {
  font-size: 1.8rem;
  margin: 0;
  font-weight: bold;
}

.nav {
  display: flex;
  gap: 2rem;
  align-items: center;
}

.nav-link {
  color: white;
  text-decoration: none;
  font-size: 1.1rem;
  padding: 0.5rem 1rem;
  border-radius: 5px;
  transition: all 0.3s;
  border: 2px solid transparent;
}

.nav-link:hover {
  background-color: rgba(255, 255, 255, 0.1);
  transform: translateY(-2px);
}

.nav-link.active {
  background-color: rgba(255, 255, 255, 0.2);
  border-color: rgba(255, 255, 255, 0.3);
  font-weight: bold;
}

.header-actions {
  display: flex;
  gap: 1rem;
  align-items: center;
}

@media (max-width: 768px) {
  .header-content {
    flex-direction: column;
    text-align: center;
  }

  .nav {
    order: 3;
    width: 100%;
    justify-content: center;
    flex-wrap: wrap;
  }

  .header-actions {
    order: 2;
  }
}
</style>
